<template>
  <div class="app-container">
    <!--表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-select v-model="searchObj.type" clearable placeholder="请选择">
          <el-option label="点餐人数统计" value="drink_num" />
          <el-option label="点餐金额数统计" value="price_count" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="searchObj.begin"
          type="date"
          placeholder="选择开始日期"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="searchObj.end"
          type="date"
          placeholder="选择截止日期"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-button
        :disabled="btnDisabled"
        type="primary"
        icon="el-icon-search"
        @click="showChart()"
      >查询</el-button>
    </el-form>
    <div class="chart-container">
      <div id="chart" class="chart" style="height:500px;width:100%" />
    </div>
  </div>
</template>
<script>
import * as echarts from 'echarts'
import daily from '@/api/statistics'
export default {
  data() {
    return {
      searchObj: {
        type: 'price_count',
        begin: '',
        end: ''
      },
      btnDisabled: false,
      chart: null,
      title: '',
      xData: [],
      yData: []
    }
  },
  created() {
    this.initDay()
    this.showChart()
  },
  methods: {
    showChart() {
      this.initChartData()
      this.setChart()
    },
    // 准备图表数据
    initChartData() {
      daily.showChart(this.searchObj).then((response) => {
        // 数据
        this.yData = response.data.data.dataList
        console.log(response.data)
        // 横轴时间
        this.xData = response.data.data.dateList
        // 当前统计类别
        switch (this.searchObj.type) {
          case 'drink_num':
            this.title = '点餐人数统计'
            break
          case 'price_count':
            this.title = '点餐金额数统计'
            break
        }
        this.setChart()
      })
    },
    // 设置图标参数
    setChart() {
      // 基于准备好的dom，初始化echarts实例
      this.chart = echarts.init(document.getElementById('chart'))
      // console.log(this.chart)
      // 指定图表的配置项和数据
      var option = {
        // x轴是类目轴（离散数据）,必须通过data设置类目数据
        xAxis: {
          type: 'category',
          data: this.xData // -------绑定数据
        },
        // y轴是数据轴（连续数据）
        yAxis: {
          type: 'value'
        },
        // 系列列表。每个系列通过 type 决定自己的图表类型
        series: [
          {
            // 系列中的数据内容数组
            data: this.yData, // -------绑定数据
            // 折线图
            type: 'line'
          }
        ],
        title: {
          text: this.title
        },
        tooltip: {
          trigger: 'axis'
        }
      }
      this.chart.setOption(option)
    },
    // 获取当前年月日
    // 获取当前时间，day为number，getDay(-1):昨天的日期;getDay(0):今天的日期;getDay(1):明天的日期;【以此类推】
    getDay(day) {
      var today = new Date()
      var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day
      today.setTime(targetday_milliseconds) // 注意，这行是关键代码
      var tYear = today.getFullYear()
      var tMonth = today.getMonth()
      var tDate = today.getDate()
      tMonth = this.doHandleMonth(tMonth + 1)
      tDate = this.doHandleMonth(tDate)
      return tYear + '-' + tMonth + '-' + tDate
    },
    doHandleMonth(month) {
      var m = month
      if (month.toString().length === 1) {
        m = '0' + month
      }
      return m
    },
    initDay() {
      this.searchObj.begin = this.getDay(-30)
      this.searchObj.end = this.getDay(0)
    }
  }
}
</script>
